package mhcs.client;

import com.google.gwt.core.ext.typeinfo.ParseException;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;

class AddModule extends VerticalPanel {
    
    public AddModule() {
    	setSize("250px", "450px");
        
        flexTable = new FlexTable();
        add(flexTable);
        flexTable.setSize("250px", "350px");
        
        Label label = new Label("Serial No.:");
        flexTable.setWidget(0, 0, label);
        serialBox = new IntegerBox();
        serialBox.setAlignment(TextAlignment.LEFT);
        flexTable.setWidget(0, 1, serialBox);
        serialBox.setSize("100px", "12px");
//        Label label_1 = new Label("Module Type:");
//        flexTable.setWidget(1, 0, label_1);
//        typeBox = new ListBox();
       // MinConfigButton minConfig = new MinConfigButton();
        //MHCS.dataSet.addListener(minConfig);
        //flexTable.setWidget(1, 1, minConfig);
//        typeBox.addItem("Plain");
//        typeBox.addItem("Dormitory");
//        typeBox.addItem("Food");
//        typeBox.addItem("Gym");
//        typeBox.addItem("Control");
//        typeBox.addItem("Power");
//        typeBox.addItem("Medical");
//        typeBox.addItem("Sanitation");
//        typeBox.addItem("Airlock");
        Label label_2 = new Label("Module Status:");
        flexTable.setWidget(1, 0, label_2);
        
        
        statusBox = new ListBox();
        flexTable.setWidget(1, 1, statusBox);
        statusBox.setWidth("100px");
        statusBox.addItem("Undamaged");
        statusBox.addItem("Damaged");
        statusBox.addItem("Undetermined");
        Label label_3 = new Label("Orientation:");
        flexTable.setWidget(2, 0, label_3);
        
        orientationBox = new ListBox();
        orientationBox.addItem("0");
        orientationBox.addItem("1");
        orientationBox.addItem("2");
        flexTable.setWidget(2, 1, orientationBox);
        orientationBox.setSize("50px", "15px");
        Label label_4 = new Label("X coordinate:");
        flexTable.setWidget(3,0, label_4);
        label_4.setWidth("90px");
        xBox = new IntegerBox();
        flexTable.setWidget(3, 1, xBox);
        xBox.setSize("50px", "15px");
        Label label_5 = new Label("Y coordinate:");
        flexTable.setWidget(4, 0, label_5);
        yBox = new IntegerBox();
        yBox.setAlignment(TextAlignment.LEFT);
        flexTable.setWidget(4, 1, yBox);
        yBox.setSize("50px", "15px");
        
        flexTable_1 = new FlexTable();
        add(flexTable_1);
        flexTable_1.setWidth("250px");
        
        addButton = new Button ("Add", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                // TODO Auto-generated method stub
                try {
//                    String type = typeBox.getValue(typeBox.getSelectedIndex());
                    String status = statusBox.getValue(statusBox.getSelectedIndex());
                    int orientation = Integer.parseInt(orientationBox.getValue(orientationBox.getSelectedIndex()));
                    int x = -1;
                    int y = -1;
                    int serial = -1;
                    try {
                        serial = serialBox.getValue();
                        x = xBox.getValue();
                        y = yBox.getValue();
                    }
                    catch (Exception e) {
                        throw new Exception("Serial number, and X & Y coordinates must be integer values");
                    }
                    if (!MHCS.dataSet.add(serial, status, orientation, x, y)) {
                        throw new Exception("That serial number or X-Y coordinate pair" +
                            " is already being used.");
                    }
                }
                catch (Exception e) {
                    Window.alert(e.getMessage());
                }
                
            }
            
        });
        flexTable_1.setWidget(0, 0, addButton);
        addButton.setWidth("53px");
        editButton = new Button ("Edit", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                // TODO Auto-generated method stub
                try {
//                    String type = typeBox.getValue(typeBox.getSelectedIndex());
                    String status = statusBox.getValue(statusBox.getSelectedIndex());
                    int orientation = Integer.parseInt(orientationBox.getValue(orientationBox.getSelectedIndex()));
                    int x = -1;
                    int y = -1;
                    int serial = -1;
                    try {
                        serial = serialBox.getValue();
                        x = xBox.getValue();
                        y = yBox.getValue();
                    }
                    catch (Exception e) {
                        throw new Exception("Serial number, and X & Y coordinates must be integer values");
                    }
                    if (!MHCS.dataSet.edit(serial, status, orientation, x, y)) {
                        throw new Exception("Module " + serial + " is not in the log, or "
                                + "the destination coordinates are taken.");
                    }
                }
                catch (Exception e) {
                    Window.alert(e.getMessage());
                }
                
            }
            
        });
        flexTable_1.setWidget(0, 1, editButton);
        editButton.setWidth("53px");
        deleteButton = new Button ("Delete", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                // TODO Auto-generated method stub
                try {
                    if (!MHCS.dataSet.remove(serialBox.getValue())) {
                        throw new Exception("Module " + serialBox.getValue() +
                                " is not in the log.");
                    }
                }
                catch (Exception e) {
                    Window.alert(e.getMessage());
                }
            }
            
        });
        flexTable_1.setWidget(0, 2, deleteButton);
        flexTable_1.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
        flexTable_1.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
        flexTable_1.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
    }
    IntegerBox serialBox;
//    ListBox typeBox;
    ListBox statusBox;
    ListBox orientationBox;
    IntegerBox xBox;
    IntegerBox yBox;
    Button addButton;
    Button editButton;
    Button deleteButton;
    private FlexTable flexTable;
    private FlexTable flexTable_1;
}
